package com.it.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.bean.OperationLog;
import com.it.mapper.OperationLogMapper;
import com.it.page.PageQuery;
import com.it.service.OperationLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Service("operationLogService")
@RequiredArgsConstructor
public class OperationLogServiceImpl implements OperationLogService {

    private final OperationLogMapper operationLogMapper;

    @Override
    public PageInfo<OperationLog> page(PageQuery<OperationLog> pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize());
        OperationLog queryBody = pageQuery.getQueryBody();
        Example example = new Example(OperationLog.class);
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(queryBody) && Objects.nonNull(queryBody.getSuccess())) {
            criteria.andEqualTo("success", queryBody.getSuccess());
        }
        example.setOrderByClause("create_time desc");
        List<OperationLog> persons = operationLogMapper.selectByExample(example);
        return new PageInfo<>(persons);
    }
}
